package com.kanshu.ksgb.fastread.doudou.module.personal.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseFragment;
import com.kanshu.ksgb.fastread.doudou.common.business.event.BindEvent;
import com.kanshu.ksgb.fastread.doudou.common.business.event.LoginEvent;
import com.kanshu.ksgb.fastread.doudou.common.business.event.RedPointEvent;
import com.kanshu.ksgb.fastread.doudou.common.business.event.TaskEvent;
import com.kanshu.ksgb.fastread.doudou.common.business.interfaces.ICancelRefreshing;
import com.kanshu.ksgb.fastread.doudou.common.business.interfaces.IMainHomeInterface;
import com.kanshu.ksgb.fastread.doudou.common.business.interfaces.ISwitchTab;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.MMKVDefaultManager;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.MMKVUserManager;
import com.kanshu.ksgb.fastread.doudou.common.business.pay.event.PayActionEvent;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.UserUtils;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.urlrouter.UrlRouter;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.StorageUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.SwipeRefreshHelper;
import com.kanshu.ksgb.fastread.doudou.common.util.ToastUtil;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.CustomDialog;
import com.kanshu.ksgb.fastread.doudou.common.view.LoadingDialog;
import com.kanshu.ksgb.fastread.doudou.common.view.TitlebarView;
import com.kanshu.ksgb.fastread.doudou.common.business.event.ReaderFinishEvent;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.BindInfo;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.PersonalAd;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.SignInData;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.UpdateInfoEvent;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.UserBean;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.UserData;
import com.kanshu.ksgb.fastread.doudou.module.personal.dialog.NewUserDialog;
import com.kanshu.ksgb.fastread.doudou.module.personal.presenter.PersonCenterPresenter;
import com.kanshu.ksgb.fastread.doudou.module.personal.presenter.PersonCenterService;
import com.kanshu.ksgb.fastread.doudou.module.signin.event.SignInEvent;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * Created by wcy on 2017\6\13 0013
 */
@SuppressLint("SetTextI18n")
@Route(path = ARouterConfig.HOME_PAGE_PERSONCENTER)
public class PersonCenterFragment extends BaseFragment implements ICancelRefreshing, IGenrialMvpView<UserBean>, ISwitchTab {
    private static final int APPOINT_DAY_NO = 0;//广告未开通
    private static final int APPOINT_DAY_YES = 1;//广告开通
    private static final int APPOINT_DAY_OVERDUE = 2;//广告已过期
    private static final int APPOINT_DAY_FOR_EVER = 3;//永久免广告
    private static final int DELAY = 3000;
    @BindView(R.id.header_img)
    ImageView mHeaderImg;
    @BindView(R.id.user_name)
    TextView mUserName;
    @BindView(R.id.mybeans)
    TextView mMybeans;
    @BindView(R.id.mycash)
    TextView mMycash;
    @BindView(R.id.user_id)
    TextView mUserId;
    @BindView(R.id.tv_ad_status)
    TextView mAdStatus;
    @BindView(R.id.swipe_refresh)
    TwinklingRefreshLayout mSwipeRefresh;
    @BindView(R.id.balance)
    TextView mBalance;
    @BindView(R.id.titleView)
    TitlebarView titlebarView;
    @BindView(R.id.lin_charge)
    LinearLayout mLinCharge;
    @BindView(R.id.charge)
    LinearLayout mCharge;
    @BindView(R.id.person_sign_red_point)
    ImageView mSignRedPoint;
    @BindView(R.id.person_settings_red_point)
    ImageView mSettingsRedPoint;
    @BindView(R.id.person_help_feedback_red_point)
    ImageView mFeedbackRedPoint;
    @BindView(R.id.login_userinfo)
    RelativeLayout login_userinfo;
    @BindView(R.id.tourists_model)
    LinearLayout tourists_model;
    @BindView(R.id.login_gift)
    ImageView login_gift;

    LinearLayout llCash;

    private AlertDialog mDialog;
    PersonCenterPresenter mPersonCenterPresenter = new PersonCenterPresenter(lifeCyclerSubject);
    private Activity mActivity;
    String mBindPhone = "";
    int mLoginState  = 0;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup v = (ViewGroup) inflater.inflate(R.layout.fragment_person_layout, container, false);
        TitlebarView titlebarView = v.findViewById(R.id.titleView);
        llCash = v.findViewById(R.id.llCash);
        ImageView searchView = new ImageView(getContext());
        searchView.setImageResource(R.mipmap.ic_search);
        titlebarView.getRightContainer().addView(searchView);
        if (titlebarView.getLeftContainer() != null) {
            titlebarView.getLeftContainer().setOnClickListener(view -> {
            });
        }
        llCash.setOnClickListener(view -> ARouterUtils.toActivity(ARouterConfig.PERSONAL_CASH_RECORD));
        mActivity = getActivity();
        searchView.setOnClickListener(view -> ARouterUtils.toActivity(ARouterConfig.SEARCH));
        ButterKnife.bind(this, v);
        init();
        checkRedPoint();
        return v;
    }

    private void loadUserInfo() {
        mLoginState = MMKVUserManager.getInstance().getLoginState();
        if(mLoginState == Constants.LoginState.TYPE_HAS_LOGIN){
            DisplayUtils.gone(tourists_model, login_gift);
            DisplayUtils.visible(login_userinfo);
        } else {
            DisplayUtils.gone(login_userinfo);
            DisplayUtils.visible(tourists_model, login_gift);
        }
        RetrofitHelper helper = RetrofitHelper.getInstance();
        Map<String, String> params = new HashMap<>();
        params.put("get_master_info", "1");
        params.put("get_account_info", "1");
        params.put("get_phone_info", "1");
        params.put("get_bind_info", "1");
        helper.createService(PersonCenterService.class)
                .getUserBaseInfo(params)
                .compose(asyncRequest())
                .subscribe(new Observer<BaseResult<UserData>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(BaseResult<UserData> userInfoBaseResult) {
                        if (userInfoBaseResult.result.data != null) {
                            updateUserInfo(userInfoBaseResult.result.data);
                            StorageUtils.setPreference(mActivity, Constants.SP_NAME, Constants.USER_BIND_PHONE + userInfoBaseResult.result.data.user_id, userInfoBaseResult.result.data.phone);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, null);
                    }

                    @Override
                    public void onComplete() {
                        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, null);
                    }
                });
    }

    private UserData userData;

    private int getUserHead(int sex) {
        int result;
        switch (sex) {
            case 1:
                result = R.mipmap.ic_edit_head_male;
                break;
            case 2:
                result = R.mipmap.ic_edit_head_female;
                break;
            default:
                result = R.mipmap.ic_edit_head_unknow;
                break;
        }
        return result;
    }

    private void updateUserInfo(UserData data) {
        if (data == null) {
            return;
        }
        this.userData = data;
        mHeaderImg.setImageResource(getUserHead(Integer.parseInt(data.sex)));
        mUserName.setText(data.nickname);
        mUserId.setText(getString(R.string.user_id, data.user_id));
        if (data.account_info == null) {
            return;
        }
        mBalance.setText(data.account_info.account_balance);
        MMKVUserManager.getInstance().setLoginBeans(data.account_info.beans_balance);
        MMKVUserManager.getInstance().setLoginSex(data.sex);
        mMybeans.setText(data.account_info.beans_balance);
        float rmb = 0;
        try {
            rmb = Float.valueOf(data.account_info.rmb_balance);
        } catch (Exception e) {
            e.printStackTrace();
        }
        @SuppressLint("DefaultLocale")
        String txtRmb = String.format("%1$.2f", rmb / 100);
        mMycash.setText(txtRmb);
        if (userData.ad != null) {
            switch (userData.ad.ad_status) {
                case PersonalAd.STATUS_CLOSED:
                    mAdStatus.setText("未开通");
                    break;
                case PersonalAd.STATUS_WORKING:
                    @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    mAdStatus.setText("已开通至 " + sdf.format(userData.ad.ad_end_time * 1000));
                    break;
                case PersonalAd.STATUS_EXPIRED:
                    mAdStatus.setText("已过期");
                    break;
                case PersonalAd.STATUS_WORKING_FOREVER:
                    mAdStatus.setText("永久开通");
                    break;
            }
        }
    }

    private void init() {
        EventBus.getDefault().register(this);
        mPersonCenterPresenter.attachView(this);
        SwipeRefreshHelper.init(mSwipeRefresh, this::loadUserInfo);
        titlebarView.setTitle("我的");
        titlebarView.getLeftContainer().removeAllViews();
        //适配状态栏
        int statusBarHeight = DisplayUtils.getStatusBarHeight(getActivity());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) titlebarView.getLayoutParams();
        params.topMargin = statusBarHeight;
        titlebarView.setLayoutParams(params);

        loadUserInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleBindEvent(BindEvent event) {
        if (event.code == BindEvent.SUCCESS) {
            if (!TextUtils.isEmpty(event.data) && userData != null) {
                mBindPhone = event.data;
                userData.phone = mBindPhone;
            }
            loadUserInfo();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handlePayActionEvent(PayActionEvent event) {
        loadUserInfo();
        //getData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleLoginEvent(LoginEvent event) {
        // 新用户 弹出新用户弹窗
        if(MMKVUserManager.getInstance().getLoginState() == Constants.LoginState.TYPE_HAS_LOGIN && TextUtils.equals(event.is_new_user, "1")){
            NewUserDialog.show(mActivity);
        }
        loadUserInfo();
        checkRedPoint();
    }

    @OnClick({R.id.tourists_model, R.id.login_gift, R.id.charge, R.id.settings, R.id.bind_cash_account, R.id.download_center,
            R.id.read_preferences, R.id.profit, R.id.encashment, R.id.sign, R.id.person_info, R.id.vOnlineService, R.id.help_feedback})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tourists_model:
            case R.id.login_gift:
                ARouterUtils.toActivity(ARouterConfig.PERSONAL_LOGIN_IN, "phone", userData == null ? "" : userData.phone);
                break;
//            case R.id.lin_charge:
//                MyAccountActivity.actionStart(mActivity);
//                break;
            case R.id.charge:
                if(mLoginState != 200){
                    ARouterUtils.toActivity(ARouterConfig.PERSONAL_LOGIN_IN, "phone", userData == null ? "" : userData.phone);
                }else {
                    ARouterUtils.toActivity(ARouterConfig.PERSONAL_CHARGE_NO_AD);
                }
                break;
            case R.id.settings:
                if(mLoginState != 200){
                    ARouterUtils.toActivity(ARouterConfig.PERSONAL_LOGIN_IN, "phone", userData == null ? "" : userData.phone);
                }else {
                    ARouterUtils.toActivity(ARouterConfig.PERSONAL_SETTINGS);
                }
                break;
            case R.id.download_center:
                if(mLoginState != 200){
                    ARouterUtils.toActivity(ARouterConfig.PERSONAL_LOGIN_IN, "phone", userData == null ? "" : userData.phone);
                }else {
                    ARouterUtils.toActivity(ARouterConfig.DOWNLOAD_DOWNLOAD_CENTER);
                }
                break;
            case R.id.read_preferences:
                ARouterUtils.toActivity(ARouterConfig.PERSONAL_READ_PREFERENCES, "isBack", false);
                break;
            case R.id.profit:
                if(mLoginState != 200){
                    ARouterUtils.toActivity(ARouterConfig.PERSONAL_LOGIN_IN, "phone", userData == null ? "" : userData.phone);
                }else {
                    ARouterUtils.toActivity(ARouterConfig.PERSONAL_PROFIT);
                }
                break;
            case R.id.encashment:
                if(mLoginState != 200){
                    ARouterUtils.toActivity(ARouterConfig.PERSONAL_LOGIN_IN, "phone", userData == null ? "" : userData.phone);
                }else {
                    int count = 0;
                    String phone = null;
                    try {
                        phone = userData.phone;
                        count = Integer.valueOf(userData.account_info.rmb_balance);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (TextUtils.isEmpty(phone)) {
                        ToastUtil.showMessage("绑定手机号后才能提现");
                        return;
                    }
                    boolean wechat = true;
                    boolean alipay = true;
                    if (userData != null) {
                        wechat = userData.weixin_extract_bind == 1;
                        alipay = userData.alipay_extract_bind == 1;
                    }
                    HashMap map = new HashMap<String,Object>();
                    if (!TextUtils.isEmpty(phone)) {
                        map.put("phone", phone);
                    }
                    map.put("alipay", alipay);
                    map.put("wechat", wechat);
                    map.put("count", count);
                    ARouterUtils.toActivity(ARouterConfig.PERSONAL_ENCASHMENT, map);
//                    EncashmentActivity.Companion.actionStart(mActivity, count, phone, alipay, wechat);
                }
                break;
            case R.id.person_info:
                ARouterUtils.toActivity(ARouterConfig.PERSONAL_USER_INFO);
                break;
            case R.id.sign:
                if(mLoginState != 200){
                    ARouterUtils.toActivity(ARouterConfig.PERSONAL_LOGIN_IN, "phone", userData == null ? "" : userData.phone);
                }else {
                    doShowSign();
                }
                break;
            case R.id.bind_cash_account:
                if(mLoginState != 200){
                    ARouterUtils.toActivity(ARouterConfig.PERSONAL_LOGIN_IN, "phone", userData == null ? "" : userData.phone);
                }else {
                    ARouterUtils.toActivity(ARouterConfig.PERSONAL_BIND_ACCOUNT);
                }
                break;
            case R.id.vOnlineService:
                showCustomServiceDialog();
                break;
            case R.id.help_feedback:
                startHelpFeedbackActivity(mActivity);
                break;
        }
    }

    /**
     * 帮助与反馈
     * @param context
     */
    public static void startHelpFeedbackActivity(Context context) {
        UrlRouter.from(context).jump(context.getString(R.string.base_jump_url) + context.getString(R.string.help_feedback));
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleRedPointEvent(RedPointEvent event) {
        checkRedPoint();
    }

    private void checkRedPoint() {
        if (getActivity() == null || getActivity().isFinishing() || mSignRedPoint == null) {
            return;
        }
        boolean signedToday = StorageUtils.getPreference(getActivity(), Constants.SP_NAME, "signed_today_" + UserUtils.getUserId(), false);
        mSignRedPoint.setVisibility(signedToday ? View.VISIBLE : View.GONE);
        boolean versionUpdate = StorageUtils.getPreference(getActivity(), Constants.SP_NAME, "version_update" + UserUtils.getUserId(), false);
        mSettingsRedPoint.setVisibility(versionUpdate ? View.VISIBLE : View.GONE);
        boolean isReadFeedback = StorageUtils.getPreference(getActivity(), Constants.SP_NAME, "is_read_feedback_" + UserUtils.getUserId(), false);
        mFeedbackRedPoint.setVisibility(isReadFeedback ? View.VISIBLE : View.GONE);
        if (getActivity() instanceof IMainHomeInterface) {
            ((IMainHomeInterface) getActivity()).setRedPoint();
        }
    }

    private void getData() {
        RetrofitHelper helper = RetrofitHelper.getInstance();
        helper.createService(PersonCenterService.class)
                .getUserInfo("1")
                .compose(asyncRequest()).subscribe(new BaseObserver<UserBean>() {

            @Override
            public void onResponse(BaseResult<UserBean> info, UserBean userBean, Disposable disposable) {
                if (info.result.data.ad != null)
                    paySuccess(mContext, info.result.data.ad.ad_end_time);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 100)
    public void handleBindEvent(BindInfo event) {
        if (!event.result) {
            return;
        }
        loadUserInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleSignInEvent(SignInEvent event) {
        loadUserInfo();
    }

    /**
     * 充值成功
     */
    private void paySuccess(Context context, long endTime) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("免广告开通成功！");
        String time = BookUtils.formatServerTime(endTime, "yyyy-M-d HH:mm");
        builder.setMessage("到期时间：" + time);
        builder.setPositiveButton("知道了", (dialog, which) -> dialog.dismiss());
        mDialog = builder.show();
        AndroidSchedulers.mainThread().scheduleDirect(mRunnable, DELAY, TimeUnit.MILLISECONDS);
    }

    //关闭AlertDialog
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (mDialog != null && getActivity() != null && !getActivity().isFinishing()) {
                mDialog.dismiss();
            }
        }
    };

    @Override
    public void cancel() {
        if (mSwipeRefresh != null) {
            mSwipeRefresh.finishRefreshing();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPersonCenterPresenter.detachView();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 显示用户信息
     */
    private void showUserInfo(String nickName, String account_balance, String headerImageUrl) {
        mUserId.setText("ID：" + UserUtils.getUserId());
        mBalance.setText(account_balance);
        String s1 = StorageUtils.getPreference(Xutils.getContext(), Constants.SP_NAME, Constants.NICK_NAME, "");
        String s2 = StorageUtils.getPreference(Xutils.getContext(), Constants.SP_NAME, Constants.HEADER_IMG, "");
        if (!TextUtils.isEmpty(s1)) {
            nickName = s1;
        }
        mUserName.setText(nickName);
    }


    @Override
    public void showContent(UserBean userBean) {
        if (userBean != null) {
            showUserInfo(userBean.nickname, userBean.account_balance, userBean.headimgurl);
            SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, null);
        }
    }

    @Override
    public void showError(int errCode, String errDesc) {
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, null);
    }

    @Override
    public void showLoading(String s) {
    }

    @Override
    public void dismissLoading() {
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleFinishEvent(ReaderFinishEvent event) {
        //mPersonCenterPresenter.getUserInfo(UserUtils.getUserId());
        loadUserInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleEditInfo(UpdateInfoEvent event) {
        loadUserInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleTaskEvent(TaskEvent event) {
        loadUserInfo();
        checkRedPoint();
    }

    private void doShowSign() {
        LoadingDialog loadingDialog = new LoadingDialog(mActivity, "");
        loadingDialog.show();
        mPersonCenterPresenter.getSignData(new BaseObserver<SignInData>() {
            @Override
            public void onResponse(BaseResult<SignInData> listBaseResult, SignInData signInBeanNews, Disposable disposable) {
                if (loadingDialog.isShowing()) {
                    loadingDialog.dismiss();
                }
                ARouter.getInstance().build(ARouterConfig.SIGN_IN_PAGE).withParcelable("data", signInBeanNews).navigation();
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                ToastUtil.showMessage("获取签到信息失败,请稍后再试");
                if (loadingDialog.isShowing()) {
                    loadingDialog.dismiss();
                }
            }
        });
    }

    public void showCustomServiceDialog() {
        showCustomServiceDialog(mActivity);
    }

    @SuppressLint("RtlHardcoded")
    public static void showCustomServiceDialog(Activity activity) {
        String info = "ID:" + UserUtils.getUserId() + "\n" + "版本：" + activity.getResources().getString(R.string.app_name) + "v" + Utils.getVersionName(activity) + "(Android)";
        CustomDialog dialog = CustomDialog.show(activity, "联系客服", "您好，请将以下显示信息提供给客服，以便我们更好地为您服务。(长按复制以下信息)\n" +
                "\n" +
                info +
                "\n\n" +
                "客服QQ：" + MMKVDefaultManager.getInstance().getCustomerServiceQQ() + "\n" +
                "在线时间：09:00-12:00 13:00-18:00", new CustomDialog.Callback() {
            @Override
            public void onSure(Dialog dialog) {
                if (dialog == null) {
                    return;
                }
                dialog.dismiss();
                try {
                    activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin=" + MMKVDefaultManager.getInstance().getCustomerServiceQQ() + "&version=1")));
                } catch (Exception e) {
                    ToastUtil.showMessage("请安装手机QQ或TIM");
                }
            }

            @Override
            public void onCancel(Dialog dialog) {
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        }, false, "取消", "在线沟通");
        dialog.getContentTextView().setGravity(Gravity.LEFT);
        dialog.getContentTextView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ClipboardManager cm = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                assert cm != null;
                cm.setText(info);
                ToastUtil.showMessage("复制成功");
                return true;
            }
        });
    }

    @Override
    public void switchTab(int tab) {

    }
}
